<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>父组件向子组件传值</title>  
    <script src="./vue.js"></script>
</head>
<!--父组件可以向子组件传值，但是子组件不能改变父组件传来的值，称为单向数据流-->
<body>
    <div id="app">
        <counter :count="3" :typeid='1' @change='handleParent'></counter>
        <counter :count="2" :typeid='2' @change='handleParent'></counter>
        <div>{{total}}</div>
    </div>
    <script>
        var counter = {
            props:['count','typeid'],
            template:'<div @click="handleClick">{{num}}</div>',
            data:function(){
                return {
                    num:this.count
                }
            },
            methods:{
                handleClick:function(){
                    if (this.typeid === 1){
                        this.num = this.num + 2;
                        this.$emit('change',2)
                    }else{
                        this.num = this.num + 3;
                        this.$emit('change',3)
                    }
                }
            }
        }
        var vm = new Vue({
            el:'#app',
            components:{
                counter:counter,
            },
            data:{
                total:5,
            },
            methods:{
                handleParent:function(step){
                    this.total = this.total + step;
                }
            }
        })
    </script>
</body>
</html>